//
//  RRZUserInfoData.m
//  RenRenZhao
//
//  Created by 邓乐 on 2017/12/1.
//  Copyright © 2017年 rrz. All rights reserved.
//

#import "EMUserInfoData.h"

static EMUserInfoData* _kUserInfoModel = nil;
@implementation EMUserInfoData

+ (NSString *)archivePath
{
    NSString * path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
    path = [path stringByAppendingPathComponent:@"MEInfo.archive"];
    return path;
}

+ (instancetype)shared
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _kUserInfoModel = [NSKeyedUnarchiver unarchiveObjectWithFile:[self archivePath]];
        if (!_kUserInfoModel) {//如果userInfo取不出来，则初始化
            _kUserInfoModel = [[EMUserInfoData alloc] init];
        }
    });
    return _kUserInfoModel;
}

#pragma mark- init code
// 直接添加以下代码即可自动完成
- (void)encodeWithCoder:(NSCoder *)aCoder { [self yy_modelEncodeWithCoder:aCoder]; }
- (id)initWithCoder:(NSCoder *)aDecoder { self = [super init]; return [self yy_modelInitWithCoder:aDecoder]; }
- (id)copyWithZone:(NSZone *)zone { return [self yy_modelCopy]; }
- (NSUInteger)hash { return [self yy_modelHash]; }
- (BOOL)isEqual:(id)object { return [self yy_modelIsEqual:object]; }
- (NSString *)description { return [self yy_modelDescription]; }

#pragma mark-private

+ (void)update
{
    [self updateInfoCompletion:nil];
}

+ (void)updateInfoCompletion:(void (^)(BOOL result))completion
{   
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        BOOL result = [NSKeyedArchiver archiveRootObject:UserInfoShared toFile:[EMUserInfoData archivePath]];
        if (completion) {
            dispatch_async(dispatch_get_main_queue(), ^{
                completion(result);
            });
        }
    });
}

+ (void)clean
{
    UserInfoShared.userId = @"";
    UserInfoShared.token = @"";
    UserInfoShared.mobile = @"";
    UserInfoShared.bindMobile = @"";
    UserInfoShared.bindEmail = @"";
    UserInfoShared.parentID = @"";
    [EMUserInfoData update];
}
 


@end
